//控制提醒等待区域margin高度------------------
var wh=window.screen.availHeight;
var marginheight=(wh-80)/2;
$("#waitplace>div").css("marginTop",marginheight+"px");
var id=$.cookie("MYID");
//环信建立连接
var conn = new WebIM.connection({
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
});
//登录
var options = {
    apiUrl: WebIM.config.apiURL,
    user:id,
    pwd:id,
    appKey: "pulu#pulu"
};
conn.open(options);
//跳转至群聊页面---------------------------------------------
function gotoallchat(e){
    if(isover==="yes"){
        location.href="chatingallpeople.html?roomid="+e.dataset.roomid+"&master="+roomandmaster[e.dataset.roomid];
        $.cookie("allchatname",chatname[e.dataset.roomid]);
    }
}
//添加回调函数 获取群组列表---------------------------------

var chatname={};//群聊名称
var allroomid=[];//群聊roomid
var roomandmaster={};//将roomID与masterid绑定
var isover="no"// 控制有没有完成
conn.listen({
    onOpened: function ( message ) {          //连接成功回调
        // 列出所有群组------------------------------------
        var option = {
            success: function (rooms) {
                //console.log(rooms);
                //如果有就加载群聊组
                if(rooms&&rooms.length>0){
                    var datanum=0;//个数
                    for(var i= 0,len=rooms.length;i<len;i++){
                        datanum++;
                        rooms[this.index]=i;
                        var allchatname="";
                        //if(rooms[i].name){
                        allchatname=rooms[i].name;
                        chatname[rooms[i].roomId]=allchatname;//群聊名称
                        allroomid.push(rooms[i].roomId);//群聊id
                        $("#newsplace").append("<div class='ts_cells' onclick='gotoallchat(this)' data-roomid='"+rooms[i].roomId+"'> <div class='listplace'> <div class='allchatimg'> <div> <img src='img/a_j.png'/> </div> </div><div class='allchatname'>"+allchatname+"</div> </div> </div>");
                        //------------------------------------------------------------------------------------------
                        //var thisnum=i;
                        //==========================================================================================
                        if(datanum===rooms.length){
                            var roomnum=0;
                            //console.log(allroomid);
                            function getmaserid(){
                                //console.log("开始执行了",allroomid.length);
                                if(roomnum===allroomid.length){
                                    //console.log(allroomid);
                                    //console.log(roomandmaster);
                                    isover="yes";
                                }else{
                                    //console.log("数字小",roomnum);
                                    conn.queryRoomInfo({
                                        roomId:allroomid[roomnum],
                                        success: function (d) {
                                            //var thisroomid=allroomid[this.index];
                                            roomandmaster[allroomid[roomnum]]=d[0].jid.match(/[pulu#pulu_0-9a-zA-Z]+/)[0].replace("pulu#pulu_","");
                                            //console.log("roomnum",roomnum,"roomandmaster[this]",roomandmaster[allroomid[roomnum]]);
                                            roomnum++;
                                            getmaserid();

                                        },
                                        error: function () {
                                            console.log('Error!');
                                        }
                                    });
                                }

                            }
                            getmaserid();
                        }
                        //var queryGroupInfo = function () {
                        //    console.log("获取群信息");
                        //    var thisnum=i;
                        //    conn.queryRoomInfo({
                        //        roomId:rooms[thisnum].roomId,
                        //        success: function (d) {
                        //            if(d&& d.length>0){
                        //                var masterid= d[0].jid.match(/[pulu#pulu_0-9a-zA-Z]+/)[0].replace("pulu#pulu_","");
                        //                console.log(masterid);
                        //                //$("#newsplace").append("<div class='ts_cells' onclick='gotoallchat(this)' data-master='"+masterid+"' data-num='"+thisnum+"'> <div class='listplace'> <div class='allchatimg'> <div> <img src='img/01.png'/> </div> </div><div class='allchatname'>"+chatname[thisnum]+"</div> </div> </div>");
                        //            }
                        //        },
                        //        error: function () {
                        //            console.log('Error!');
                        //        }
                        //    });
                        //};
                        //queryGroupInfo();
                        //==========================================================================================
                        //}
                    }
                }
            },
            error: function () {
                console.log('List groups error');
            }
        };
        conn.listRooms(option);
    },
    onClosed: function ( message ) {},         //连接关闭回调
    onTextMessage: function ( message ) {},    //收到文本消息
    onEmojiMessage: function ( message ) {},   //收到表情消息
    onPictureMessage: function ( message ) {}, //收到图片消息
    onCmdMessage: function ( message ) {},     //收到命令消息
    onAudioMessage: function ( message ) {},   //收到音频消息
    onLocationMessage: function ( message ) {},//收到位置消息
    onFileMessage: function ( message ) {},    //收到文件消息
    onVideoMessage: function (message) {},     //收到视频消息
    onPresence: function ( message ) {},       //收到联系人订阅请求、处理群组、聊天室被踢解散等消息
    onRoster: function ( message ) {},         //处理好友申请
    onInviteMessage: function ( message ) {},  //处理群组邀请
    onOnline: function () {},                  //本机网络连接成功
    onOffline: function () {},                 //本机网络掉线
    onError: function ( message ) {},          //失败回调
    onBlacklistUpdate: function (list) {       //黑名单变动
                                               // 查询黑名单，将好友拉黑，将好友从黑名单移除都会回调这个函数，list则是黑名单现有的所有好友信息
        console.log(list);
    }
});
//主页面返回键----------------------------------------------
$("#mainpagegobackbtn").click(function(){
    //location.href="chatpage.html";
    history.go(-1);
});
//
$("#addallchatbtn").click(function(){
    location.href="createallchat.html";
});